/**
* StringExt.js
* 
* @author Demis Bellot
* @version 1.0
* @requires Class.js
*
* Copyright 2009, Demis Bellot
* http://code.google.com/p/ajaxstack/
*/

function StringExt() { } 
var S = StringExt;
S.OVERFLOW_SUFFIX = "..";
S.ESCAPE_CHARS_REGEX = new RegExp("([{}\(\)\^$&.\*\?\/\+\|\[\\\\]|\]|\-)", "g");
S.upperCaseFirstChar = function(fieldName)
{
	if (!fieldName) return fieldName;
	return fieldName.charAt(0).toUpperCase() + fieldName.substr(1);
};
S.escapeRegexChars = function(s)
{
	var newString = s.replace(S.ESCAPE_CHARS_REGEX, "\\$1");
	return newString;
};
S.startsWith = function(text, startsWith)
{
	if (!text || !startsWith) return false;
	if (startsWith.length > text.length) return false;
	return text.substring(0, startsWith.length) == startsWith;
};
S.startsWithAny = function(text, startsWithArray)
{
	for (var i = 0; i < startsWithArray.length; i++)
	{
		if (S.startsWith(text, startsWithArray[i])) return true;
	}
	return false;
};
S.escapeHtml = function(str)
{
	if (!str) return str;
	if (!is.String(str)) str = str.toString();
	str = str.replace(/&/g, "&amp;");
	str = str.replace(/</g, "&lt;");
	str = str.replace(/>/g, "&gt;");
	return str;
};
S.cropText = function(text, lineLength, testLength)
{
	testLength = isNaN(testLength) ? text.length : testLength;
	return testLength <= lineLength ? text : text.substr(0, lineLength) + S.OVERFLOW_SUFFIX;
};
S.chop = function(text, length)
{
	length = length || 1;
	return text.substring(0, text.length - length);
};
S.equals = function(to, from)
{
	if (!to || !from) return false;
	to = is.String(to) ? to : to.toString();
	from = is.String(from) ? from : from.toString();
	return to == from;
};
S.pad = function(text, padLen, padChar, lpad)
{
	var padChar = padChar || "0";
	text = text.toString();
	var s = text;
	for (var i = text.length; i < padLen; i++)
	{
		s = lpad 
			? padChar + s 
			: s + padChar;
	}
	return s;
}
S.padLeft = function(text, padLen, padChar)
{
	return S.pad(text, padLen, padChar, true);
}
S.padRight = function(text, padLen, padChar)
{
	return S.pad(text, padLen, padChar, false);
}
S.lpad = S.padLeft;
S.rpad = S.padRight;
S.createAttr = function(kvp)
{
	var attrName, text = "";
	for (attrName in kvp)
	{
		if (text) text += " ";
		var attrValue = kvp[attrName] || "";
		text += attrName + '="' + attrValue.toString().replace('"', '\\"') + '"';
	}
	return text;
};
S.eat = function(text, until, greedy)
{
	var pos = greedy ? text.lastIndexOf(until) : text.indexOf(until);
	return (pos != -1) ? text.substring(pos + until.length) : null;
};
S.cat = function()
{
	var all = "";
	for (var i = 0, len = arguments.length; i < arguments.length; i++)
	{
		all += arguments[i];
	}
	return all;
}
S.toString = function()
{
	if (arguments.length == 0 || !arguments[0]) return null;

	var s = "";
	for (var i = 0; i < arguments.length; i++)
	{
		var arg = arguments[i];

		if (s) s += "/";

		if (is.String(arg)) s += arg;
		else if (is.ValueType(arg)) s += arg.toString();
		else if (is.Array(arg)) s += '[' + A.join(arg, ",") + ']';
		else
		{
			var o = "";
			for (var name in arg)
			{
				if (o) o += ",";
				o += name + ":" + S.safeString(arg[name]);
			}
			s += '{' + o + '}';
		}
	}
	return s;
}
S.safeString = function(str)
{
	if (!str) return str;
	if (S.containsAny(str, ['[', ']', '{', '}', ',']))
	{
		return '"' + str + '"';
	}
	return str;
}
S.trim = function(str, chars)
{
	return S.ltrim(S.rtrim(str, chars), chars);
}
S.ltrim = function(str, chars)
{
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
S.rtrim = function(str, chars)
{
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
S.contains = function(str, test)
{
    if (!is.String(str)) return;
    return str.indexOf(test) != -1;
}
S.containsAny = function(str, tests)
{
    if (!is.String(str)) return;
	for (var i = 0, len = tests.length; i < len; i++)
	{
		if (str.indexOf(tests[i]) != -1) return true;
	}
	return false;
}
S.r4 = function()
{
	return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
S.createGuid = function()
{
	return (S.r4() + S.r4() + "-" + S.r4() + "-" + S.r4() + "-" + S.r4() + "-" + S.r4() + S.r4() + S.r4());
}
S.strip = function(text, match)
{
	return text.replace(match, "");
}
